package PageTable;

import PageClassTable.AddPageClass;
import utils.JdbcUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class UpdataPage {
    static String sql_query_id="select * from page where page_id = ?";
    static String sql_updata_id="UPDATE page SET page_name = ? , page_class = ? ,page_content = ? ,page_update = ? WHERE page_id = ?";
    public static void updata(int power,String name){
        JFrame frame=  new JFrame("修改");
        String info = JOptionPane.showInputDialog(frame,"你要修改的文章id号","修改",JOptionPane.WARNING_MESSAGE);
        if(info!=null){
            //设置窗体大小
            frame.setSize(800, 600);
            //设置窗体居中
            frame.setLocationRelativeTo(null);
            //取消布局
            frame.setLayout(null);
            //设置窗口不能改变大小
            frame.setResizable(false);
            //设置窗体是否可见
            frame.setVisible(true);
            int id = Integer.parseInt(info);
            List<Map<String, Object>> re= JdbcUtils.JdbcQuery(sql_query_id,id);
            String name_page = null;
            String class_ = null;
            String content = null;
            LocalDateTime betime = null;
            LocalDateTime updatetime = null;
            String username = null;
            for (Map<String, Object> map : re) {
                for (String key : map.keySet()) {
                    if (key.equals("page_name")) name_page = (String) map.get(key);
                    if (key.equals("page_class")) class_ = (String) map.get(key);
                    if (key.equals("page_content")) content = (String) map.get(key);
                    if (key.equals("page_releasedata")) {
                        Object value = map.get(key);
                        if (value instanceof Timestamp) {
                            betime = ((Timestamp) value).toLocalDateTime();
                        }
                    }
                    if (key.equals("page_update")) {
                        Object value = map.get(key);
                        if (value instanceof Timestamp) {
                            updatetime = ((Timestamp) value).toLocalDateTime();
                        }
                    }
                    if (key.equals("page_permissions")) power = (int) map.get(key);
                    if (key.equals("page_user")) username = (String) map.get(key);
                }
            }
            Box textid=Box.createHorizontalBox();
            JLabel textid_a = new JLabel("文章id为:"+id);
            textid.add(textid_a);
            textid.add(Box.createHorizontalStrut(10));
            textid.setBounds(30,50,700,30);
            frame.add(textid);

            Box textuser=Box.createHorizontalBox();
            JLabel textuser_a=new JLabel("文章作者为:"+username);
            textuser.add(textuser_a);
            textuser.add(Box.createHorizontalStrut(10));
            textid.setBounds(30,100,700,30);
            frame.add(textid);

            Box texttimebe=Box.createHorizontalBox();
            JLabel texttimebe_a = new JLabel("发布时间id为:"+betime);
            texttimebe.add(texttimebe_a);
            texttimebe.add(Box.createHorizontalStrut(10));
            texttimebe.setBounds(30,150,700,30);
            frame.add(texttimebe);

            Box texttime=Box.createHorizontalBox();
            JLabel texttime_a = new JLabel("上次修改时间id为:"+updatetime);
            texttime.add(texttime_a);
            texttime.add(Box.createHorizontalStrut(10));
            texttime.setBounds(30,200,700,30);
            frame.add(texttime);

            Box textname=Box.createHorizontalBox();
            JLabel textname_a = new JLabel("文章名为为:"+name_page+"  修改为:");
            JTextField textname_b=new JTextField(15);
            textname.add(textname_a);
            textname.add(Box.createHorizontalStrut(10));
            textname.add(textname_b);
            textname.setBounds(30,250,700,30);
            frame.add(textname);

            JComboBox<String> textclass_b= new JComboBox<>();
            List<Map<String, Object>> datas= JdbcUtils.JdbcQuery("SELECT page_class_name FROM page_class");
            for(Map<String,Object> map : datas){
                for(String key : map.keySet()) {
                    textclass_b.addItem((String) map.get(key));
                }
            }
            textclass_b.addItem("添加新类");
            Box textclass_=Box.createHorizontalBox();
            JLabel textclass_a = new JLabel("文章分类为:"+class_+"  修改为:");
            textclass_.add(textclass_a);
            textclass_.add(Box.createHorizontalStrut(10));
            textclass_.add(textclass_b);
            textclass_.setBounds(30,300,700,30);
            frame.add(textclass_);
            Box textcontent=Box.createHorizontalBox();
            JTextArea textcontent_a = new JTextArea(2,30);
            textcontent_a.append("原文为:\n"+content);
            textcontent_a.setLineWrap(true);// 激活自动换行功能
            textcontent_a.setEditable(false); //只读状态
            JScrollPane croll1 = new JScrollPane(textcontent_a);
            JTextArea textcontent_b=new JTextArea(2,30);
            textcontent_b.setLineWrap(true);// 激活自动换行功能
            JScrollPane croll2 = new JScrollPane(textcontent_b);
            textcontent.add(croll1);
            textcontent.add(Box.createHorizontalStrut(10));
            textcontent.add(croll2);
            textcontent.setBounds(30,350,700,80);
            frame.add(textcontent);

            //按钮盒子
            Box btnbox=Box.createHorizontalBox();
            //提交修改按钮
            JButton subBtn=new JButton("修改");
            //取消修改按钮
            JButton noBtn=new JButton("返回");
            btnbox.add(subBtn);
            btnbox.add(Box.createHorizontalStrut(60));
            btnbox.add(noBtn);
            btnbox.setBounds(450,450,700,50);
            frame.add(btnbox);

            noBtn.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int yn=JOptionPane.showConfirmDialog(frame,"确定退出吗？","提示", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);	//确认对话框
                    if(yn==JOptionPane.OK_OPTION) frame.dispose();
                }
            });
            String finalName_page = name_page;
            String finalClass_ = class_;
            String finalContent = content;
            subBtn.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int yn=JOptionPane.showConfirmDialog(frame,"确定提交修改吗？","提示", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                    if(yn==JOptionPane.OK_OPTION) {
                        LocalDateTime time=LocalDateTime.now();
                        String a=textname_b.getText().trim();
                        if(a.isEmpty())a= finalName_page;
                        String b= Objects.requireNonNull(textclass_b.getSelectedItem()).toString();
                        if(Objects.equals(b, "添加新类")) {
                            b = AddPageClass.init(1);
                        }
                        String c=textcontent_b.getText().trim();
                        if(c.isEmpty())c= finalContent;
                        JdbcUtils.JdbcUpdate(sql_updata_id,a,b,c,time,id);
                        JOptionPane.showMessageDialog(frame,"修改成功");
                        frame.dispose();
                    }
                }
            });
        }
    }
}
